package org.tp8.sampleapp.service.impl;

import javax.jws.WebService;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tp8.sampleapp.exc.SampleAppException;
import org.tp8.sampleapp.service.abs.AbstractService;
import org.tp8.sampleapp.service.intf.WorkflowService;
import org.tp8.sampleapp.util.SampleAppLogger;

/**
 * The Class CourseServiceImpl.
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service(value = "workflowService")
@WebService(endpointInterface = "org.tp8.sampleapp.service.intf.WorkflowService")
public class WorkflowServiceImpl extends AbstractService implements
		WorkflowService {

	/**
	 * The Constant log.
	 */
	public final static SampleAppLogger log = new SampleAppLogger(
			WorkflowServiceImpl.class);

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private RepositoryService repositoryService;

	@Override
	public String create(String wfwName) throws SampleAppException {
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey(wfwName);
		log.debug("Created WFW with id: " + wfwName + " for : " + wfwName);
		return processInstance.getProcessInstanceId();
	}

	@Override
	public void terminate(String wfwId) throws SampleAppException {
		log.debug("Deleting wfwId: " + wfwId);
		runtimeService.deleteProcessInstance(wfwId, "WS_TERMINATION");
	}

}
